<?php 
/*
 * @Descripttion: Dictionary
 * @version: v1
 * @Author: Terry
 * @Date: 2020-09-15 16:27:31
 * @LastEditors: Terry
 * @LastEditTime: 2024-02-04 13:58:09
 */
namespace libs;
use think\facade\Db;
use think\facade\Cache;

class Dictionary{

	static function getDic($array=[]){
		
		if(empty($array)) return $array;
		foreach($array as $key => $value){
			$data[$value] =  self::getData($value);
		}
		return $data;
	}

	private static function getData($key){
		if($key == 'area')  return self::getArea();
		// $dic = Cache::store('redis')->get('dic_'.$key);
		if(empty($dic)){
			$tablename = 'dic'.ucfirst($key);
			$dic = DB::table($tablename)->withoutField('id')->select()->toArray();
			Cache::store('redis')->set('dic_'.$key,$dic);
		}
		return $dic;
	}

	/**
	 * @Descripttion: 获取管理员类型
	* @Author: Terry
	* @Date: 2021-11-08 15:52:35
	* @param {*} $corptype
	*/
	public static function getManageType($corptype){
		$key  = 'manageType'.$corptype;
		$dic = Cache::store('redis')->get('dic_'.$key);
		if(!$dic||empty($dic)){
			$dic = DB::table('dicManageType')->where('corpType','like',"%{$corptype}%")->field(['code','type'])->select()->toArray();
			Cache::store('redis')->set('dic_'.$key,$dic);
		}
		return $dic;
	}
	/**
	 * @Description: 获取区域
	* @Author: Terry
	* @Date: 2022-11-23 17:47:24
	*/
	public static function  getArea(){
		$dir  =  app()->getRootPath()."/dic/";
        $path  = $dir."area.txt";
		if(is_file($path)){
			$res = file_get_contents($path);
			if(!$res) return json_decode($path,true);
		} 
		if(!makeDir($dir)) return false;
		$dic = DB::table('dicArea')->withoutField('id')->select()->toArray();
		$area = self::areaProcess($dic);
		file_put_contents($path,json_encode($area));
		return $area;
	}


	    /**
     * @Descripttion: 获取预警类型
     * @Author: Terry
     * @Date: 2021-11-08 15:49:26
     * @param {*} $type 预警类型 1=>人员预警 2=》扬尘设备
     */    
    public static function getWarningtype($type){
		$key  = 'warningtype_'.$type;
		$dic = Cache::store('redis')->get('dic_'.$key);
		if(!$dic || empty($dic)){
			$dic = DB::table('dicWarningType')->where('warningType','like',"%{$type}%")->field(['code','type'])->select()->toArray();
			Cache::store('redis')->set('dic_'.$key,$dic);
		}
		return $dic;
    }


	/**
  * @Descripttion: v1 处理为三级联动关系(后期建议优化代码)
  * @name: areaProcess
  * @Author: Terry
  * @Date: 2021-02-04 00:41:53
  * @param {*} $dic
  */
	private static function areaProcess($dic){
		if(!$dic) return $dic;
		//处理层级关系
		foreach ($dic as $key => $value) {
			if(strlen($value['code'])==2){
				$list[$value['code']] = $value;
			}
			if(strlen($value['code'])==4){
				$provinceKey =  substr($value['code'],0,2);
				$list[$provinceKey]['children'][$value['code']] = $value;
			}
			if(strlen($value['code'])==6){
				$provinceKey =  substr($value['code'],0,2);
				if(isset($list[$provinceKey]['children'])&&!isset($list[$provinceKey]['children'][0])){
					$cityKey =  substr($value['code'],0,4);
					$list[$provinceKey]['children'][$cityKey]['children'][$value['code']] = $value;
				}else{
					$list[$provinceKey]['children'][] = $value;	
				}
				
			}
		}

		//把下标key处理为0开始，否则前端无法识别
		$porv = 0;
		foreach ($list as $key => $value) {
			$res[$porv] = [
				'area'=>$value['area'],
				'code'=>$value['code'],
			];
			$city = 0;
			if(isset($list[$key]['children'])){
				foreach ($value['children'] as $kk => $vv) {
					//有可能出现code为6位的市级地区(不属于区)
					if(!isset($vv['area'])){
						foreach($vv['children'] as $kkSon=>$vvSon){
							$res[$porv]['children'][$city] = [
								'area'=>$vvSon['area'],
								'code'=>$vvSon['code'],
							];;
							$city++;
						}
					//普通code为4位的市级城市
					}else{
						$res[$porv]['children'][$city] =  [
							'area'=>$vv['area'],
							'code'=>$vv['code'],
						];
						if(isset($vv['children'])){
							foreach($vv['children'] as $kkk => $vvv){
								$res[$porv]['children'][$city]['children'][] = $vvv;
							}
						}
					}
					$city++;
				}
			}
			$porv++;
		} 
		return $res;
	}
	
}
?>